package com.carey.myblog.controller.admin;


import com.carey.myblog.pojo.*;
import com.carey.myblog.service.BlogService;
import com.carey.myblog.service.BlogTagsService;
import com.carey.myblog.service.TagService;
import com.carey.myblog.service.TypeService;
import com.carey.myblog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("admin")
public class BlogsController {

    private static final String INPUT="admin/blogs-input";
    private static final String LIST="admin/blogs";
    private static final String REDIRECT_LIST="redirect:/admin/blogs";

    @Autowired
    private BlogService blogService;

    @Autowired
    private BlogTagsService blogTagsService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    @GetMapping("blogs")
    public String blogs(Pageable pageable, BlogQuery blog, Model model){
        model.addAttribute("types",typeService.list());
        model.addAttribute("page",blogService.listBlogforadmin(pageable,blog));
        return "admin/blogs";
    }

    @PostMapping("/blogs/search")
    public String search(Pageable pageable, BlogQuery blog, Model model){
        model.addAttribute("page",blogService.listBlogforadmin(pageable,blog));
        return "admin/blogs ::blogList"; //返回admin/blogs页面下的一个片段blogList,实现局部刷新
    }

    @GetMapping("/blogs/input")
    public String input(Model model){
        setTypeAndTag(model);
        model.addAttribute("blog",new Blog());
        return INPUT;
    }

    public void setTypeAndTag(Model model){
        model.addAttribute("types",typeService.list()); //获取所有分类
        model.addAttribute("tags",tagService.listTag());  //获取所有标签
    }

    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable Long id, Model model){
        setTypeAndTag(model);
        Blog blog=blogService.getBlog(id);
        blog.init(); //初始化，将标签数组转换为字符串
        model.addAttribute("blog",blog);
        return INPUT;
    }

    @PostMapping("/blogs")
    public String post(Blog blog, RedirectAttributes attributes, HttpSession session){
        User user = (User) session.getAttribute("user");
        blog.setUserId(user.getId());
        Type type = blog.getType();
        blog.setTypeId(type.getId());
        String tagIds = blog.getTagIds();
        //blog.setTypeId(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.listTag(blog.getTagIds()));
        System.out.println("-------"+blog);
        int b;
        if (blog.getId() == null) {
            if (StringUtils.isEmpty(blog.getFlag())){
                blog.setFlag("原创");
            }
            b =  blogService.saveBlog(blog);//id为空，执行新建操作
            blog.setTags(blogTagsService.convertToList(tagIds));
            BlogTags blogTags = new BlogTags();
            blog.getTags().forEach(tag -> {
                blogTags.setTagsId(tag.getId());
                blogTags.setBlogsId(blog.getId());
                blogTagsService.save(blogTags);
            });
        } else {
            if (StringUtils.isEmpty(blog.getFlag())){
                blog.setFlag("原创");
            }
            b = blogService.updateBlog(blog.getId(), blog); //id有值，执行更新操作
            blog.setTags(blogTagsService.convertToList(tagIds));
            blogTagsService.delete(blog.getId());
            BlogTags blogTags = new BlogTags();
            blog.getTags().forEach(tag -> {
                blogTags.setTagsId(tag.getId());
                blogTags.setBlogsId(blog.getId());
                blogTagsService.save(blogTags);
            });
        }
        if(b == 0){
            attributes.addFlashAttribute("message","操作失败");
        }else {
            attributes.addFlashAttribute("message","操作成功");
        }
        return REDIRECT_LIST;
    }

    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes){
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message","删除成功");
        return REDIRECT_LIST;
    }

}
